package com.clkj.xiuzheng.service.shangcheng;

import com.clkj.xiuzheng.dao.shangcheng.PondMapper;
import com.clkj.xiuzheng.dao.shangcheng.ScVipMapper;
import com.clkj.xiuzheng.dao.shangcheng.ScVipLevelNumMapper;
import com.clkj.xiuzheng.pojo.common.DateFormatHelper;
import com.clkj.xiuzheng.pojo.common.Result;
import com.clkj.xiuzheng.pojo.shangcheng.Pond;
import com.clkj.xiuzheng.pojo.shangcheng.VipLevel;
import com.clkj.xiuzheng.pojo.shangcheng.VipLevelNum;
import com.clkj.xiuzheng.pojo.shangcheng.dto.VipLevelDto;
import com.clkj.xiuzheng.pojo.shangcheng.vo.VipLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service
public class ScVipServiceImpl implements ScVipService{

    @Autowired
    private ScVipMapper scVipMapper;
    @Autowired
    private ScVipLevelNumMapper scvipLevelNumMapper;
    @Autowired
    private PondMapper pondsMapper;
    @Override
    public List<VipLevelVo> getVipList(Integer companyId) {
        //获取列表中所有等级
        Set<VipLevelNum> level = scVipMapper.getLevelByCompanyId(companyId);
        List<VipLevelVo> leveList = new ArrayList<>();
        for (VipLevelNum vipLevelNum : level) {
            //List<VipLevel> list =   scVipMapper.selectByLevelIdAndCompanyId(vipLevelNum.getVipLevelNumber(),companyId);
            Example example = new Example(VipLevel.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("levelId",vipLevelNum.getLevelId());
            criteria.andEqualTo("companyId",companyId);
            List<VipLevel> list = scVipMapper.selectByExample(example);
            VipLevelVo vipLevelVo = new VipLevelVo();
            vipLevelVo.setLevel(vipLevelNum.getVipLevelNumber());
            vipLevelVo.setText(vipLevelNum.getLevelName());
            vipLevelVo.setNextScore(vipLevelNum.getNextScore());
            vipLevelVo.setLevelId(vipLevelNum.getLevelId());
            vipLevelVo.setChecked(false);
            vipLevelVo.setRules(list);
            leveList.add(vipLevelVo);
        }
       /* for (int i=0;i<array.length;i++){
            List<VipLevel> list =   scVipMapper.selectByLevelIdAndCompanyId((Integer)array[i],companyId);
            VipLevelVo vipLevelVo = new VipLevelVo();
            vipLevelVo.setLevel((Integer) array[i]);
            if (list.size() != 0){
                vipLevelVo.setText(list.get(0).getLevelName());
            }else {
              String name = scvipLevelNumMapper.setNameByLevel((Integer)array[i],companyId);
                vipLevelVo.setText(name);
            }
            Example example = new Example(VipLevelNum.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("vipLevelNumber",(Integer)array[i]);
            criteria.andEqualTo("companyId",companyId);
            vipLevelVo.setNextScore(scvipLevelNumMapper.selectOneByExample(example).getNextScore());
            vipLevelVo.setLevelId(list.get(0).getLevelId());
            vipLevelVo.setChecked(false);
            vipLevelVo.setRules(list);
            leveList.add(vipLevelVo);
        }*/
        return leveList;
    }

    @Override
    public void addVipLevel(VipLevelDto vipLevelDto) {
        //添加等级和返利活动 type = 0
        if (vipLevelDto.getType() == 0) {
            //添加等级之前先看看等级是否已存在
            Example example = new Example(VipLevelNum.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("vipLevelNumber",vipLevelDto.getLevel());
            criteria.andEqualTo("companyId",vipLevelDto.getCompanyId());
            VipLevelNum vipLevelNum1 = scvipLevelNumMapper.selectOneByExample(example);
            //这个公司的这个等级已经存在 返回已存在
            if (vipLevelNum1!=null) {
               throw new RuntimeException("该等级已存在");
            }
            //添加等级
            VipLevelNum vipLevelNum = new VipLevelNum();
            vipLevelNum.setCompanyId(vipLevelDto.getCompanyId());
            vipLevelNum.setVipLevelNumber(vipLevelDto.getLevel());
            vipLevelNum.setLevelName(vipLevelDto.getLevelText());
            vipLevelNum.setNextScore(vipLevelDto.getNextScore());
            scVipMapper.addVipLevel(vipLevelNum);
            //如果有返利活动 添加返利活动
            if (vipLevelDto.getIsAdd() == 1) {
                vipLevelDto.getList().forEach(vipLevel -> {
                    vipLevel.setCompanyId(vipLevelDto.getCompanyId());
                    vipLevel.setLevelId(vipLevelNum.getLevelId());
                    vipLevel.setLevelStatus(1);
                });
                scVipMapper.insertList(vipLevelDto.getList());
            }

        }//只添加返利活动 type = 1
        else {
            if (vipLevelDto.getList().size() != 0) {
                Example example = new Example(VipLevelNum.class);
                Example.Criteria criteria = example.createCriteria();
                //前台只反了等级 这里根据等级查id
                criteria.andEqualTo("vipLevelNumber",vipLevelDto.getLevel());
                criteria.andEqualTo("companyId",vipLevelDto.getCompanyId());
                VipLevelNum vipLevelNum = scvipLevelNumMapper.selectOneByExample(example);
                //把查到的id放进去
                vipLevelDto.getList().forEach(vipLevel -> {
                    vipLevel.setCompanyId(vipLevelDto.getCompanyId());
                    vipLevel.setLevelId(vipLevelNum.getLevelId());
                    vipLevel.setLevelStatus(1);
                });
                scVipMapper.insertList(vipLevelDto.getList());
            }
        }
    }

    @Override
    public List<VipLevelNum> getVipLevelList(Integer companyId) {
        Example example = new Example(VipLevelNum.class);
        example.createCriteria().andEqualTo("companyId",companyId);
        example.orderBy("vipLevelNumber").asc();
        List<VipLevelNum> vipLevelNums = scvipLevelNumMapper.selectByExample(example);

        return vipLevelNums;
    }

    @Override
    public Map listVipLevelByUser() {
        List<VipLevel> level = scvipLevelNumMapper.listVipLevelByUser();
        Pond pond  = pondsMapper.getNewPond();
        Map  map = new HashMap<>();
        map.put("level",level);
        map.put("pond",pond);
        return map;

    }

    @Override
    public void updataStatusByVipId(Integer vipId, Integer type) {
        VipLevel vipLevel = new VipLevel();
        vipLevel.setVipId(vipId);
        vipLevel.setLevelStatus(type);
        scVipMapper.updateByPrimaryKeySelective(vipLevel);
    }


    @Override
    public void editVipLevelNum(VipLevelNum vipLevelNum) {
        scvipLevelNumMapper.updateEditVipLevelNum(vipLevelNum);
    }

    @Override
    public void deleteVipLevelNum(Integer levelId) {
        scvipLevelNumMapper.deleteScvipLevelNum(levelId);
    }

    @Override
    public void editVipLevel(VipLevel vipLevel) {
        scVipMapper.updateEditVipLevel(vipLevel);
    }

    @Override
    public void deleteVipLevel(Integer vipId) {
        scVipMapper.deleteVipLevel(vipId);
    }

    @Override
    public void addPond(Pond pond) {
        pond.setDateTime(DateFormatHelper.currentTimes());
        pondsMapper.insertSelective(pond);
    }

    @Override
    public List<Pond> listPond() {
        Example example = new Example(Pond.class);
        example.orderBy("pondId").desc();
        return pondsMapper.selectByExample(example);
    }

    @Override
    public void updatePond(Integer pondId, Integer status) {
        Example example = new Example(Pond.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("pondId",pondId);
        Pond pond = new Pond();
        pond.setPondStatus(status);
        pondsMapper.updateByExampleSelective(pond,example);
    }
}
